﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;
using GameSchool.ViewModels;

namespace GameSchool.Controllers
{
    public class LevelController : Controller
    {
		LevelRepository repository = new LevelRepository();
		GameRepository grep = new GameRepository();
        //
        // GET: /Level/

        public ActionResult Index()
        {
            return View();
        }

		public ActionResult Levels(int? id)
		{

			var items = from n in repository.Levels()
						orderby n.ID
						select n;
			return View(items);
		}

		/* Aðeins umsjónarmaður
		* Hér fyrir neðan eru 2 controllerar.
		* Þeir sjá um að birta formið "LevelForm.cshtml"
		*/

		[HttpGet]
		[Authorize(Roles = "Admin, Teacher")]
		public ActionResult createLevel(int id)
		{
			ViewData["currGame"] = id;
			var test = ((from n in grep.Games()
					   where n.ID == id
					   select n).SingleOrDefault()).Name;
			ViewData["gameName"] = test;
			TempData["currUrl"] = ("/Game/ViewGame/"+id).ToString();
			return View(new Level()); 
		}


		[HttpPost]
		[Authorize(Roles = "Admin, Teacher")]
		public ActionResult createLevel(FormCollection formData)
		{
			Level newLevel = new Level();
			repository.Add(newLevel);
			UpdateModel(newLevel);
			//setja inn validation
			repository.Save();
			return Redirect("../.." + TempData["currUrl"]);
		}

		[HttpGet]
		[Authorize(Roles = "Admin, Teacher")]
		public ActionResult editLevel(int? id)
		{
			//has.value ? villutékk vantar
			var item = (from n in repository.Levels()
						where n.ID == id.Value
						select n).SingleOrDefault();
	
			if (item != null)
			{
				return View(item);
			}
			else
			{
				return RedirectToAction("Levels");
			}
		}


		/* Aðeins umsjónarmaður
		 * Þessi fall breytir leiknum og vistar í gagnagrunn
		 * 
		 * 
		 */

		[HttpPost]
		[Authorize(Roles = "Admin")]
		public ActionResult editLevel(int? id, FormCollection formData)
		{
			//has.value ? villutékk vantar hjá mér
			//IEnumerable<Game> item = from n in repository.Games()
			var item = (from n in repository.Levels()
						where n.ID == id.Value
						select n).SingleOrDefault();

			if (item != null)
			{
				//Geri breytingar
				UpdateModel(item);


				//Vista breytingar
				repository.Save();
				return RedirectToAction("Levels");
			}
			else
			{
				return View();
			}
		}

		[HttpGet]
		public ActionResult viewLevel(int? id)
		{
			LevelRepository a = new LevelRepository();
			AssignmentRepository assRep = new AssignmentRepository();
			VideoRepository vidRep = new VideoRepository();

			var model = new ViewLevelViewModel
			{
				Level = (from n in repository.Levels()
						where n.ID == id.Value
						select n).SingleOrDefault(),

				Levels = from n in a.Levels()
								where n.GameID == id
								select n,

				Assignments = from n in assRep.Assignments()
							  where n.LevelID == id
							  select n,

				Videos = from n in vidRep.Videos()
						 where n.LevelID == id
						 select n
			};
			return View(model);
		}
    }
}
